$.views.converters({
	fgoodsStatus : function(value) {
		if(!value){
			return "";
		}
		for(index in stats_goodsStatus){
			var goodsStatus = stats_goodsStatus[index];
			if(goodsStatus.dictCode == value){
				return goodsStatus.dictName;
			}
		}
		return  "未知类型("+value+")";
	},

	fapplyStatus : function(value){
		if(!value){
			return "";
		}
		for(index in stats_approves){
			var approves = stats_approves[index];
			if(approves.dictCode == value){
				return approves.dictName;
			}
		}
		return  "未知类型("+value+")";
	},
	fmoneyUnit : function(value){
		if(!value){
			return "";
		}
		for(index in stats_currencys){
			var currencys = stats_currencys[index];
			if(currencys.dictCode == value){
				return currencys.dictName;
			}
		}
		return  "未知类型("+value+")";
	}
});
var goodsStatsCtrl  = new XFApp.client();
XFApp.client.prototype.goodsStats = {
	successCode : "10000",
	page : null,
	hisPage : null,
	hisId : 0,
	cusPage : null,
	urlItems : {
		queryByPageURL : basePath+"/packagestats/goodsStats/queryPage",
		delGoodsRUL : basePath +"/packagestats/goodsRecord/delGoods",
		queryCustomerByPageURL : basePath + "/packagestats/goodsStats/queryCusPage",
		queryByHisPageURL : basePath + "/packagestats/goodsStats/queryHistoryPage"
	},
	
	queryPage : function(){
		_this = this;
		var params = {
			itemno : $("#itemno").val(),
			itemnameFuzzy : $("#itemname").val(),
			customerCode : $("#customercode").val(),
			docketstatus : $("#goodsDocketStatus").attr("data-id")
		}
		if(!!_this.page){
			_this.page.page("remote",0,params);
		}else{
			var currentPageIndex = 0 ;
			if(!!$("#cur_pageNo").val()){
				currentPageIndex = parseInt($("#cur_pageNo").val());
			}
			_this.page = $("#page").page({
				pageSize : 10,
				currentPageIndex:currentPageIndex,
				remote: {
					url: _this.urlItems.queryByPageURL,
			        params: params, 
			        success: function (data, pageIndex) {
			        	$("#cur_pageNo").val(pageIndex);
			        	if(data.code==_this.successCode){
			        		var pageResult = data.result;
			        		if(pageResult.itemList.length>0){
			        			for(var index in pageResult.itemList){
			        				var element = pageResult.itemList[index];
			        				element["seqNum"]=parseInt(pageResult.pageSize)*(parseInt(pageResult.pageNo)-1)+parseInt(index)+1;
			        			}
			        			$("#page_grid").html($("#dataTmp").render(pageResult.itemList));
			        		}else{
			        			$("#page_grid").html('<tr><td align="center" colspan="11">暂无数据</td></tr>');
			        		}
			        	}else{
			        		webDialogUtil.simpleAlert(data.msg, "温馨提示"); 
			        	}
			        },
			        error : function(){
			        	webDialogUtil.simpleAlert("系统出现异常,请联系开发人员", "温馨提示"); 
			        }
				},
				firstBtnText : '首页',
				lastBtnText : '尾页',
				prevBtnText : '上一页',
				nextBtnText : '下一页',
				showInfo : true,
				showJump : true,
				jumpBtnText : '跳转',
				showPageSizes : true,
				infoFormat : '共{pageCount}页,{total}项'
			});
		}
	},
	queryHisPage : function(id){
		_this = this;
		var params = {
			id :id
		}
		if(!!_this.hisPage){
			_this.hisPage.page("remote",0,params);
		}else{
			_this.hisPage = $("#history_page").page({
				pageSize : 5,
				remote: {
					url: _this.urlItems.queryByHisPageURL,
			        params: params, 
			        success: function (data, pageIndex) {
			        	if(data.code==_this.successCode){
			        		var pageResult = data.result;
			        		if(pageResult.itemList.length>0){
			        			for(var index in pageResult.itemList){
			        				var element = pageResult.itemList[index];
			        				element["seqNum"]=parseInt(pageResult.pageSize)*(parseInt(pageResult.pageNo)-1)+parseInt(index)+1;
			        			}
			        			$("#his_page_grid").html($("#hisDataTmp").render(pageResult.itemList));
			        		}else{
			        			$("#his_page_grid").html('<tr><td align="center" colspan="11">暂无数据</td></tr>');
			        		}
			        	}else{
			        		webDialogUtil.simpleAlert(data.msg, "温馨提示"); 
			        	}
			        },
			        error : function(){
			        	webDialogUtil.simpleAlert("系统出现异常,请联系开发人员", "温馨提示"); 
			        }
				},
				firstBtnText : '首页',
				lastBtnText : '尾页',
				prevBtnText : '上一页',
				nextBtnText : '下一页',
				showInfo : true,
				showJump : true,
				jumpBtnText : '跳转',
				showPageSizes : true,
				infoFormat : '共{pageCount}页,{total}项'
			});
			_this.hisId = id;
		}
	},
	delGoods : function(id){
		webDialogUtil.simpleConfrimAlert('是否确认删除？', function(){
		    	$.ajax({
					url:_this.urlItems.delGoodsRUL,
					data:{id:id},//JSON.stringify(docket),
					dataType:'json',
					type:'post',
					success:function(data){
						if(data.code!=10000){
							webDialogUtil.simpleAlert(data.msg, "温馨提示"); 
						}else{
							_this.queryPage();
						}
					},
					error:function(){
						webDialogUtil.simpleAlert("删除出现异常,请联系开发人员", "温馨提示"); 
					}
				});
		});
	},
	queryCusPage : function(){
		_this = this;
		var params = {
			customerCode : $("#customercodeFilter").val(),
			customerName : $("#customernameFilter").val()
		}
		
		if(!!_this.cusPage){
			_this.cusPage.page("remote",0,params);
		}else{
			
			_this.cusPage = $("#customer_page").page({
				pageSize : 5,
				remote: {
					url: _this.urlItems.queryCustomerByPageURL,
			        params: params, 
			        success: function (data, pageIndex) {
			        	if(data.code==_this.successCode){
			        		var pageResult = data.result;
			        		$("#customer_data_grid").html($("#cusDataTmp").render(pageResult.itemList));
			        	}else{
			        		webDialogUtil.simpleAlert(data.msg, "温馨提示"); 
			        	}
			        },
			        error : function(){
			        	webDialogUtil.simpleAlert("数据查询异常，请联系开发人员", "温馨提示"); 
			        }
				},
				firstBtnText : '首页',
				lastBtnText : '尾页',
				prevBtnText : '上一页',
				nextBtnText : '下一页',
				showInfo : true,
				showJump : true,
				jumpBtnText : '跳转',
				showPageSizes : true,
				infoFormat : '共{pageCount}页'
			});
		}
	},
	selCustomer : function(value){
		$("#customercode").val(value);
		$("#choose_customer_modal").modal('hide'); 
	},
	queryHistory : function(id){
		_this.queryHisPage(id);
		$("#choose_history_modal").modal('show'); 
	},
	event : function(){
		_this = this;
		$("#goods_search").on("click",function(){
			_this.queryPage();
		});
		$("#customercode_choose").on("click",function(){
			$("#choose_customer_modal").modal();
		});
		$("#custom_serach").on("click",function(){
			_this.queryCusPage();
		});
	
		$("#custom_reset").on("click",function(){
			$("input[name='customercodeFilter']").val("");
			$("input[name='customernameFilter']").val("");
		});
	},
};
$(function(){
	
	var news_goodsStatus = [];
	for(var index = 0 ;index<stats_goodsStatus.length;index++){
		var tmpStatus = stats_goodsStatus[index];
		if(tmpStatus.dictName!="暂存"){
			news_goodsStatus.push(tmpStatus);
		}
	}
	$("#goodsDocketStatus").bsSuggest({
		idField: "dictCode",  
		keyField: "dictName",
		effectiveFields :["dictCode","dictName"],
		effectiveFieldsAlias:{dictCode:"编码",dictName:"名称"},
       data: {
           'value':news_goodsStatus,
       }
   });
	goodsStatsCtrl.goodsStats.event();
	goodsStatsCtrl.goodsStats.queryPage();
});
